﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using ConsolidationTool.Common;
using System.Web.UI.WebControls;

namespace ConsolidationTool.WebParts.Layouts.CReport.Brochure
{
    public partial class R_BrochureMergeOutput : FSReportBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }


        public override void ExportReport_Click(object sender, EventArgs e)
        {
            // SPLongOperation lop = new SPLongOperation(this.Page);
            //  lop.Begin();
            BeforLoad();
            // todo
            Dictionary<string, string> mappingTemplate = new Dictionary<string, string>();
            mappingTemplate.Add("R_Brochure_C", "Asia Management report template.xlsx");
            mappingTemplate.Add("R_Brochure_A", "Asia Management report template.xlsx");

            string selectedType = hfReportTypeValue.Value;
            string selectedTemplate = mappingTemplate[selectedType];

            var selectedReports = new List<string>();
            foreach (ListItem item in cblReportType.Items)
            {
                if (item.Selected)
                    selectedReports.Add(item.Value);
            }

            var brcList = SPContext.Current.Web.Lists["BRC"];
            var colToOutput = new List<SPListItem>();
            int itemCount = brcList.Items.Count;

            for (int i = 0; i < itemCount; i++)
            {
                var item = brcList.Items[i];
                if ((item["Category"] + "").Contains(selectedType) && selectedReports.Contains(item.Title))
                    colToOutput.Add(item);
            }

            ReportMaker pm = new ReportMaker();
            var reports = pm.InitReportConfig(CEntity, colToOutput.GetEnumerator());
            string url = OpenXMLExcelBusiness.ARTMergeReportCreate(CEntity, reports, selectedTemplate, true);
            // END long-running operation

            // lop.End(this.CurrentRequestUrlAndQuery, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context,"d="+ url);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", string.Format("<script>window.open('" + url + "');</script>"));

        }
    }
}
